﻿using DataAccess.DAL;
using DataAccess.DTO;
using DataAccess.Models;
using DataAccess.Solr;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Agent.Controllers
{
    public class AgentController : BaseController
    {



        

        //申请代理
        public ActionResult Apply()
        {
            return View();
        }

        /// <summary>
        /// 代理商品库
        /// </summary>
        /// <returns></returns>
        public ActionResult Product()
        {
            ViewBag.MenuType = 2;
            ViewBag.user_name = UserSession.user_name;

            return View();
        }

        //代理订单
        public ActionResult Order()
        {
            return View();
        }
        [HttpPost]
        public JsonResult OrderPost(int limit, int type = 0, int timetype = 0)
        {
            OrdersDAL dal = new OrdersDAL();

            string where = " 1=1 ";
            switch (type)
            {
                case 1:
                    where += " and OrderStateDes='订单付款' ";
                    break;
                case 2:
                    where += " and OrderStateDes='订单结算' ";
                    break;
                case 3:
                    where += " and OrderStateDes='订单失效' ";
                    break;

            }
            switch (timetype)
            {
                case 1:
                    where += " and createdate ='" + DateTime.Now.ToString("yyyy-MM-dd") + "' ";

                    break;
                case 2:
                    where += " and createdate ='" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + "' ";

                    break;
                case 3:
                    where += " and createdate >'" + DateTime.Now.ToString("yyyy-MM-01") + "' ";

                    break;
            }
            string orderbys = "";
            int count = 0;

            List<Orders> tbkItems = new DataAccess.DAL.OrdersDAL().ListPage(AgencyUsers.promoter_id,where, orderbys, 10, limit, out count);

            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = tbkItems;
            return Json(result);


        }

        //提现
        public ActionResult TX()
        {
            return View();
        }

        //提现
        public ActionResult Withdraw()
        {
            //获取可提现金额
            //
            AgencyIncomeDAL aiDal = new AgencyIncomeDAL();
            ViewBag.withdraw_count=  aiDal.GetCanApplyIncome(UserSession.user_id);

           // ViewBag.withdraw_count = 100.00;
            ViewBag.withdraw_type = 0;//（就这里是0） 1支付宝 2银行卡

            WithdrawDAL dal = new WithdrawDAL();
            var list= dal.GetWithdraw(UserSession.user_id.ToString());
            var defModel=list.FirstOrDefault(l => l.is_default==1);
            if(defModel!=null)
            {
                ViewBag.withdraw_type = defModel.type;
            }
            
            return View(list);
        }
        //设置银行信息
        public JsonResult PostWithdraw(Withdraw model)
        {

            Result res = new Result() { State = false, Message = "添加失败" };
            if (model != null)
            {
                WithdrawDAL dal = new WithdrawDAL();

                //判断帐号存在否
                if (dal.Exist(UserSession.user_id, model.type))
                {
                    res.Message = "已添加过该账户";
                    return Json(res);
                }

                var withdraw = new Withdraw();
                if (model.type == 1 && !string.IsNullOrEmpty(model.account_name) && !string.IsNullOrEmpty(model.account_number))
                {
                   //支付宝
                    withdraw.account_name = model.account_name;
                    withdraw.account_number = model.account_number;
                    withdraw.au_id = UserSession.user_id;
                    withdraw.create_date = DateTime.Now;
                    withdraw.is_del = false;
                    withdraw.opening_bank = "";
                    withdraw.type = 1;

                }else if (model.type == 2 && !string.IsNullOrEmpty(model.account_name) && !string.IsNullOrEmpty(model.account_number) && !string.IsNullOrEmpty(model.opening_bank))
                {
                    //银行卡
                    withdraw.account_name = model.account_name;
                    withdraw.account_number = model.account_number;
                    withdraw.au_id = UserSession.user_id;
                    withdraw.create_date = DateTime.Now;
                    withdraw.is_del = false;
                    withdraw.opening_bank = model.opening_bank;
                    withdraw.type = 2;
                }
                else
                {
                    res.State = false;
                    res.Message = "提交的信息不正确";
                    return Json(res);
                }

                if (withdraw != null && dal.Insert(withdraw))
                {
                    res.State = true;
                    res.Message = "账号添加成功";
                }
            }
            return Json(res);
        }

        [HttpPost]
        public JsonResult PostWithdrawApply(string wd_type)
        {
            Result res = new Result() {State=false,Message="操作失败" };
            //申请提现
            AgencyIncomeDAL dal = new AgencyIncomeDAL();
            if (dal.ApplyWithdraw(UserSession.user_id, wd_type))
            {
                res.State = true;
                res.Message = "申请成功";
            }
            return Json(res);
         }


        //提现记录
        public ActionResult TXRecord()
        {
            AgencyIncomeDAL dal = new AgencyIncomeDAL();
            ViewBag.MenuType = 0;
            string where=" is_apply_pay=1 and au_id=@au_id ";

            int count = 0;
            var list = dal.ListPage(where, " id desc ", 10, 1, out count, new { au_id=UserSession.user_id });

            return View(list);
        }


        // 本月汇总
        public ActionResult HZ()
        {
            ProductEffectDAL dal = new ProductEffectDAL();
            string sql = "select sum(paynum) paynum,sum(estimate) estimate,sum(income) income from [ProductEffect]where createdate>'" + DateTime.Now.ToString("yyyy-MM-01") + "' and username='" + UserSession.user_name + "'  group by UserName  ";


            var model = dal.GetBySql<ProductEffectInfoDTO>(sql);
            if (model == null)
            {
                model = new ProductEffectInfoDTO();
                model.PayNum = "0";
                model.Estimate = 0;
                model.Income = 0;
            }
            return View(model);
        }



        [HttpPost]
        public JsonResult GetEffectInfo(int limit = 1)
        {
            ProductEffectDAL dal = new ProductEffectDAL();

            string where = " username='" + UserSession.user_name + "' ";

            where += " and createdate>'" + DateTime.Now.ToString("yyyy-MM-01") + "'";

            int count = 0;
            var list = dal.ListPage<ProductEffectInfoDTO>(" createdate,promotionbit,paynum,estimate,income,username  ", "[ProductEffect]", where, "", "", " createdate desc ", 20, limit, out count);
            list.ToList().ForEach(l =>
            {
                l.CreateDate = (l.CreateDate.Split(' ')[0]);
                l.PromotionBit = l.PromotionBit.Contains("wx") ? "微信" : l.PromotionBit.Contains("qq") ? "QQ" : l.PromotionBit.Contains("wz") ? "网站" : "";

            });

            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = list;
            return Json(result);
        }




        //推荐注册代理
        public ActionResult QrCode()
        {
            return View();
        }

        /// <summary>
        /// 我的朋友
        /// </summary>
        /// <returns></returns>
        public ActionResult Huoban()
        {
            return View();
        }


        /// <summary>
        /// 获取伙伴
        /// </summary>
        /// <param name="type">1 朋友  2朋友的朋友 </param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GetHuoBan(int type = 1, int limit = 1)
        {
            AgencyUsersDAL dal = new AgencyUsersDAL();

            int count = 0;
            string where = " 1=1 ";
            if (type == 1)
            {
                where += " and superior=" + UserSession.user_id;
            }
            else
            {
                where += "and onthe_superior=" + UserSession.user_id;

            }


            var list = dal.ListPage<AgencyUsersDTO>(" id,username,nickname,head_img", "[AgencyUsers]", where, "", "", " add_data desc ", 10, limit, out count);


            Result result = new Result();
            result.State = true;
            result.Message = "查询成功";
            result.Datas = list;
            return Json(result);

        }




        /// <summary>
        /// 获取代理产品库
        /// </summary>
        /// <returns></returns>
        public ActionResult DLProduct()
        {
            #region MyRegion
            //#region MyRegion
            //return Content("{\"status\":1,\"content\":[{\"id\":\"1410056\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u5929\\u5929\\u7279\\u4ef7\\u590f\\u97e9\\u7248\\u5973\\u5916\\u7a7f\\u8d85\\u5f39\\u8367\\u5149\\u4e94\\u5206\\u6253\\u5e95\\u88e4\\u663e\\u7626\\u5927\\u7801\\u8584\\u4e94\\u5206\\u88e4\\u5149\\u6cfd\\u88e4\",\"istmall\":\"0\",\"dsr\":\"\",\"quan_id\":\"d28931404eb246d395a21184569aea2f\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee139\\u5143\",\"org_price\":\"39.90\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?#!\\/promo\\/self\\/campaign?campaignId=31526801&shopkeeperId=28938948&userNumberId=748725868&tab=2\",\"num_iid\":\"20724503188\",\"pic_url\":\"https:\\/\\/img.alicdn.com\\/imgextra\\/i2\\/1790635482\\/TB2XXzqsrBmpuFjSZFAXXaQ0pXa_!!1790635482.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"24.90\",\"tk_rate\":\"30.00\",\"yongjin\":\"7.47\",\"goods_sale\":\"515\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"9856\",\"coupons_take\":\"144\",\"coupons_price\":\"15\",\"coupons_start\":\"\",\"coupons_end\":\"1495382399\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?seller_id=748725868&activity_id=d28931404eb246d395a21184569aea2f\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u9ad8\\u5f39\\u4e0d\\u6613\\u52fe\\u4e1d\\uff0c\\u5149\\u6cfd\\u4e2a\\u6027\\u8bbe\\u8ba1\\uff0c\\u6027\\u611f\\u663e\\u7626\\uff0c\\u7a7f\\u51fa\\u5c0f\\u957f\\u817f\\uff0c\\u591a\\u6b3e\\u591a\\u8272\\u53ef\\u9009\\uff0c2\\u6761\\u8d85\\u503c\\u62a2\\u8d2d\\uff0c\\u9001\\u8fd0\\u8d39\\u9669\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"3.74\"},{\"id\":\"1410055\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u65b0\\u77ed\\u8896\\u5706\\u9886\\u788e\\u82b1\\u96ea\\u7eba\\u5988\\u5988\\u88c5\\u4fee\\u8eab\\u77e5\\u6027\\u5957\\u88c5\\u4e2d\\u957f\\u4e2d\\u8001\\u5e74\\u5973\\u4e24\\u4ef6\\u5957\\u8fde\\u8863\\u88d9\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"e3787b56284b4047aba3af25b8a15c89\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee1248\\u5143\",\"org_price\":\"249.00\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?#!\\/promo\\/self\\/campaign?campaignId=46677530&shopkeeperId=58265008&userNumberId=2096676346&tab=2\",\"num_iid\":\"547895096625\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/bao\\/uploaded\\/i3\\/2096676346\\/TB293HwmctnpuFjSZFKXXalFFXa_!!2096676346.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"129.00\",\"tk_rate\":\"30.00\",\"yongjin\":\"38.70\",\"goods_sale\":\"30\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"952\",\"coupons_take\":\"48\",\"coupons_price\":\"120\",\"coupons_start\":\"\",\"coupons_end\":\"1495295999\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?seller_id=2096676346&activity_id=e3787b56284b4047aba3af25b8a15c89\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u7cbe\\u9009\\u9762\\u6599\\uff0c\\u4f18\\u8d28\\u4eb2\\u80a4\\uff0c\\u89e6\\u89c9\\u4e1d\\u6ed1\\uff0c\\u7ec6\\u817b\\u7eb9\\u7406\\uff0c\\u624b\\u611f\\u987a\\u6ed1\\uff0c\\u5e26\\u7ed9\\u4f60\\u4eb2\\u80a4\\u4eab\\u53d7\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"19.35\"},{\"id\":\"1410054\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u5987\\u708e\\u6d01\\u79c1\\u5904\\u62a4\\u7406\\u6db2\\u79c1\\u5bc6\\u5904\\u6d17\\u6db2\\u6291\\u83cc\\u5987\\u79d1\\u6e05\\u6d17\\u6db2\\u6d01\\u9634\\u5973\\u6027\\u9634\\u9053\\u51b2\\u6d17\\u5668\\u6db2\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"3adaad76ebe04da192015dff41b4afff\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee144\\u5143\",\"org_price\":\"49.90\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?spm=a220o.1000855.0.0.7c2uLY#!\\/promo\\/self\\/campaign?campaignId=44319292&shopkeeperId=117402166&userNumberId=2959723515\",\"num_iid\":\"545086753823\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i2\\/2959723515\\/TB2YoGFkOlnpuFjSZFgXXbi7FXa_!!2959723515.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"29.90\",\"tk_rate\":\"30.50\",\"yongjin\":\"9.12\",\"goods_sale\":\"848\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"4997\",\"coupons_take\":\"3\",\"coupons_price\":\"20\",\"coupons_start\":\"\",\"coupons_end\":\"1495295999\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=2959723515&activityId=3adaad76ebe04da192015dff41b4afff\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u5927\\u5bb9\\u91cf2\\u74f6\\u88c5\\uff0c\\u7ecf\\u5178\\u690d\\u7269\\u8349\\u672c\\u539f\\u6db2\\uff0c18\\u5e74\\u4fe1\\u8d56\\u89c1\\u8bc1\\uff0c\\u5987\\u708e\\u6d01\\uff0c\\u6d17\\u6d17\\u66f4\\u5065\\u5eb7\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"4.56\"},{\"id\":\"1410053\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u7c73\\u8f9b\\u8fea \\u65b0\\u751f\\u513f\\u8863\\u670d\\u548c\\u5c1a\\u670d\\u5a74\\u513f\\u8863\\u670d\\u7eaf\\u68c9\\u5957\\u88c5\\u5b9d\\u5b9d\\u4fdd\\u6696\\u5185\\u8863\\u6625\\u79cb\\u51ac\\u5b63\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"f32e58b6a6704e4983e6a3cd5853e65b\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee178\\u5143\",\"org_price\":\"79.00\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?#!\\/promo\\/self\\/campaign?campaignId=36505359&shopkeeperId=45721830&userNumberId=1697649947&tab=2\",\"num_iid\":\"40122846470\",\"pic_url\":\"https:\\/\\/img.alicdn.com\\/imgextra\\/i3\\/1697649947\\/TB289a4qXXXXXaPXFXXXXXXXXXX_!!1697649947.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"29.00\",\"tk_rate\":\"35.50\",\"yongjin\":\"10.30\",\"goods_sale\":\"38\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"9674\",\"coupons_take\":\"326\",\"coupons_price\":\"50\",\"coupons_start\":\"\",\"coupons_end\":\"1495382399\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?seller_id=1697649947&activity_id=f32e58b6a6704e4983e6a3cd5853e65b\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"A\\u7c7b\\u54c1\\u8d28\\uff0c\\u5929\\u7136\\u5f69\\u68c9\\uff0c\\u65e0\\u9aa8\\u7f1d\\u5236\\uff0c\\u7ec6\\u5fc3\\u5475\\u62a4\\u5b9d\\u5b9d\\uff0c\\u4eb2\\u80a4\\u8212\\u9002\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"5.15\"},{\"id\":\"1410052\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"0\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u7761\\u88d9\\u5973\\u590f\\u5b63\\u7eaf\\u68c9\\u6027\\u611f\\u5bbd\\u677e\\u77ed\\u8896\\u5bb6\\u5c45\\u670d\\u6625\\u79cb\\u751c\\u7f8e\\u53ef\\u7231\\u4e2d\\u8896\\u4e94\\u5206\\u8896\\u7761\\u8863\\u5973\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"60aa3ad6d70a4396812ddb2165c68dc6\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee169\\u5143\\u53ef\\u7528\",\"org_price\":\"69.00\",\"dingxianurl\":\"\",\"num_iid\":\"546853457674\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i4\\/TB1mmN4QXXXXXc5XVXXXXXXXXXX_!!0-item_pic.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"39.00\",\"tk_rate\":\"30.50\",\"yongjin\":\"11.90\",\"goods_sale\":\"530\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"9734\",\"coupons_take\":\"266\",\"coupons_price\":\"30\",\"coupons_start\":\"\",\"coupons_end\":\"1495382399\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=3106464415&activityId=60aa3ad6d70a4396812ddb2165c68dc6\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u6625\\u590f\\u79cb\\u4e09\\u5b63\\u53ef\\u7a7f\\uff0c\\u7eaf\\u68c9\\u8d85\\u597d\\u54c1\\u8d28\\uff0c\\u5bbd\\u677e\\u4f11\\u95f2\\uff0c\\u65f6\\u5c1a\\u8774\\u8776\\u7ed3\\uff0c\\u53ef\\u7231\\u7684\\u8774\\u8776\\u7ed3\\u649e\\u4e0a\\u661f\\u661f\\uff0c\\u8425\\u9020\\u51fa\\u8212\\u9002\\u68a6\\u5e7b\\u7684\\u611f\\u89c9\\uff0c\\u53ea\\u4e3a\\u60a8\\u7684\\u54c1\\u8d28\\u751f\\u6d3b\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"5.95\"},{\"id\":\"1410051\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"0\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u7ba1\\u7406\\u4e66\\u7c4d \\u7ba1\\u7406\\u8d8a\\u7b80\\u5355\\u8d8a\\u597d\\uff08\\u51685\\u518c\\uff09\\u4f01\\u4e1a\\u7ba1\\u7406\\u4e66\\u7c4d \\u9886\\u5bfc\\u6267\\u884c\\u529b\\u7ba1\\u7406 \\u9500\\u552e\\u7ba1\\u7406 \\u8425\\u9500\\u7ba1\\u7406\\u4eba\\u529b\\u8d44\\u6e90\\u884c\\u653f\\u7ba1\\u7406\\u56e2\\u961f\\u7ba1\\u7406\\u7545\\u9500\\u4e66\\u7c4d\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"326ee6a2e63540c8869b026e30b9aee9\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee164\\u5143\\u53ef\\u7528\",\"org_price\":\"65.00\",\"dingxianurl\":\"\",\"num_iid\":\"524121689127\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i1\\/656930399\\/TB2TtN0b2SM.eBjSZFNXXbgYpXa_!!656930399.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"45.00\",\"tk_rate\":\"30.50\",\"yongjin\":\"13.73\",\"goods_sale\":\"18\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"978\",\"coupons_take\":\"22\",\"coupons_price\":\"20\",\"coupons_start\":\"\",\"coupons_end\":\"1504195199\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=656930399&activityId=326ee6a2e63540c8869b026e30b9aee9\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u5982\\u4f55\\u505a\\u4e00\\u540d\\u9886\\u5bfc\\uff0c\\u5feb\\u901f\\u5347\\u804c\\uff0c\\u5404\\u79cd\\u573a\\u5408\\u4e0b\\u7684\\uff0c\\u8bf4\\u8bdd\\u6280\\u5de7\\uff0c\\u4eba\\u9645\\u4ea4\\u6d41\\uff0c\\u9886\\u5bfc\\u529b \\u6267\\u884c\\u529b \\u7ba1\\u7406\\u5b66\\u539f\\u7406\\uff0c\\uff08\\u51685\\u518c\\uff09\\u4e0d\\u6ee1\\u610f\\u968f\\u65f6\\u9000\\u6362\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"6.86\"},{\"id\":\"1410050\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"2\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u7537\\u7ae5\\u77ed\\u88e4\\u590f\\u88c52017\\u65b0\\u6b3e\\u97e9\\u7248\\u513f\\u7ae5\\u88e4\\u5b50\\u4e03\\u5206\\u88e4\\u4f11\\u95f2\\u88e4\\u7537\\u5b69\\u590f\\u5b63\\u6f6e\\u5927\\u7ae5\\u88c5\",\"istmall\":\"0\",\"dsr\":\"\",\"quan_id\":\"c6b3562f7afa4f50b8d804f4b71560b6\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee144\\u5143\\u53ef\\u7528\",\"org_price\":\"49.00\",\"dingxianurl\":\"\",\"num_iid\":\"545669179809\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i4\\/77822280\\/TB2u2b1k4BmpuFjSZFsXXcXpFXa_!!77822280.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"34.00\",\"tk_rate\":\"28.00\",\"yongjin\":\"9.52\",\"goods_sale\":\"7262\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"2671\",\"coupons_take\":\"2329\",\"coupons_price\":\"15\",\"coupons_start\":\"\",\"coupons_end\":\"1495382399\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=77822280&activityId=c6b3562f7afa4f50b8d804f4b71560b6\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u7cbe\\u9009\\u4f18\\u8d28\\u9762\\u6599\\uff0c\\u8212\\u9002\\u67d4\\u8f6f\\uff0c\\u5065\\u5eb7\\u4eb2\\u80a4\\uff0c\\u5bbd\\u677e\\u4e14\\u6709\\u5f39\\u6027\\uff0c\\u65f6\\u5c1a\\u767e\\u642d\\uff0c\\u8ba9\\u60a8\\u7684\\u5b69\\u5b50\\u4ece\\u5c0f\\u5c31\\u8d70\\u5411\\u65f6\\u4ee3\\u7684\\u6f6e\\u6d41\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"4.76\"},{\"id\":\"1410049\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u5929\\u7136\\u871c\\u8721\\u6212\\u6307 \\u94f6\\u954018k\\u9ec4\\u91d1\\u9576\\u5d4c\\u6d3b\\u53e3\\u7425\\u73c0\\u9e21\\u6cb9\\u9ec4\\u8001\\u871c\\u8721\\u7537\\u5973\\u6b3e\\u8840\\u73c0\\u6212\",\"istmall\":\"0\",\"dsr\":\"\",\"quan_id\":\"ca46aa1432884a0eb930c6f5801af3d4\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee158\\u5143\\u53ef\\u7528\",\"org_price\":\"58.90\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?spm=2013.1.iteminfo.12.GVOIn4#!\\/promo\\/self\\/campaign?campaignId=52061218&shopkeeperId=13989150&userNumberId=52901166\",\"num_iid\":\"543063420541\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i2\\/52901166\\/TB2IvhUadRopuFjSZFtXXcanpXa_!!52901166.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"38.90\",\"tk_rate\":\"30.00\",\"yongjin\":\"11.67\",\"goods_sale\":\"218\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"2937\",\"coupons_take\":\"63\",\"coupons_price\":\"20\",\"coupons_start\":\"\",\"coupons_end\":\"1495295999\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=52901166&activityId=ca46aa1432884a0eb930c6f5801af3d4\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u4e8c\\u4ee3\\u871c\\u8721\\u662f\\u5929\\u7136\\u871c\\u8721\\u8fb9\\u89d2\\u6599\\u538b\\u5236\\u5408\\u6210\\uff0c\\u80fd\\u6d6e\\u76d0\\u6c34\\uff0c\\u7d2b\\u5149\\u706f\\u7167\\u5c04\\u6709\\u8367\\u5149\\uff0c\\u8ddf\\u5929\\u7136\\u7684\\u7279\\u6027\\u662f\\u4e00\\u6837\\u7684\\uff0c\\u5916\\u89c2\\u4e0a\\u4e5f\\u662f\\u4e00\\u6a21\\u4e00\\u6837\\u7684\\u4e86\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"5.84\"},{\"id\":\"1410048\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"1\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u6377\\u666e\\u83f2\\u7f8e\\u53d1\\u9f3b\\u6bdb\\u526a\\u5200 \\u7f8e\\u5bb9\\u5957\\u88c5 \\u6307\\u7532\\u526a 5\\u4ef6\\u5957 \\u7ecf\\u6d4e\\u5b9e\\u60e0\\u6b3e\\u5f0f\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"911a5c5b6ec04a9c938f0061a66c6d00\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee119\\u5143\\u53ef\\u7528\",\"org_price\":\"19.90\",\"dingxianurl\":\"http:\\/\\/pub.alimama.com\\/myunion.htm?spm=a219t.7900221\\/10.1998910289.d059f97a4.LhVmou#!\\/promo\\/self\\/campaign?campaignId=55578374&shopkeeperId=59627146&userNumberId=2092022199\",\"num_iid\":\"543542756416\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i1\\/2092022199\\/TB2KvcIbbBmpuFjSZFAXXaQ0pXa_!!2092022199.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"9.90\",\"tk_rate\":\"15.00\",\"yongjin\":\"1.49\",\"goods_sale\":\"26\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"9969\",\"coupons_take\":\"31\",\"coupons_price\":\"10\",\"coupons_start\":\"\",\"coupons_end\":\"1510675199\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=2092022199&activityId=911a5c5b6ec04a9c938f0061a66c6d00\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u7cbe\\u7f8e\\u968f\\u8eab\\u7f8e\\u5bb9\\u5957\\uff0c\\u950b\\u5229\\u65e0\\u6bd4\\uff0c\\u9002\\u5408\\u5927\\u4eba\\u513f\\u7ae5\\u4f7f\\u7528\\uff0c\\u7b80\\u6613\\u6613\\u6e05\\u6d17\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"0.74\"},{\"id\":\"1410047\",\"weid\":\"13\",\"px\":\"0\",\"zy\":\"2\",\"tj\":\"0\",\"zd\":\"0\",\"qf\":\"0\",\"type\":\"0\",\"lxtype\":\"0\",\"yjtype\":\"2\",\"show_type\":\"0\",\"title\":\"\\u4e0d\\u9508\\u94a2\\u591a\\u5c42\\u88e4\\u67b6\\u5b50\\u4f38\\u7f29\\u591a\\u529f\\u80fd\\u9b54\\u672f\\u88e4\\u67b6S\\u578b\\u591a\\u5c42\\u8863\\u67dc\\u6302\\u8863\\u67b6\\u88e4\\u67b6\\u9632\\u6ed1\",\"istmall\":\"1\",\"dsr\":\"\",\"quan_id\":\"0f3fbff32bf54b3891abbe9981f29539\",\"quan_condition\":\"\\u5355\\u7b14\\u6ee116\\u5143\\u53ef\\u7528\",\"org_price\":\"19.90\",\"dingxianurl\":\"\",\"num_iid\":\"45644281558\",\"pic_url\":\"http:\\/\\/img.alicdn.com\\/imgextra\\/i4\\/2260305225\\/TB2iZ.Da1tTMeFjSZFOXXaTiVXa_!!2260305225.jpg\",\"small_images\":\"\",\"item_url\":\"\",\"shop_title\":\"\",\"yprice\":\"0.00\",\"price\":\"14.90\",\"tk_rate\":\"30.50\",\"yongjin\":\"4.54\",\"goods_sale\":\"54820\",\"nick\":\"\",\"tk_durl\":\"\",\"click_url\":\"\",\"taokouling\":\"\",\"coupons_total\":\"4488\",\"coupons_take\":\"10512\",\"coupons_price\":\"5\",\"coupons_start\":\"\",\"coupons_end\":\"1495295999\",\"coupons_url\":\"http:\\/\\/shop.m.taobao.com\\/shop\\/coupon.htm?sellerId=2260305225&activityId=0f3fbff32bf54b3891abbe9981f29539\",\"coupons_tkl\":\"\",\"provcity\":\"\",\"tjcontent\":\"\\u6dd8\\u62a2\\u8d2d\\u7279\\u4ef7\\uff0c\\u70ed\\u95004\\u4e07\\u4ef6\\u88e4\\u67b6\\uff0c\\u53ef\\u63025\\u6761\\u88e4\\u5b50\\uff0c\\u4e0d\\u9508\\u94a2\\u6750\\u8d28\\uff0c\\u8ba9\\u8863\\u67dc\\u4e0d\\u518d\\u62e5\\u6324\\uff0c\\u88e4\\u5b50\\u4e0d\\u518d\\u51cc\\u4e71\\uff0c\\u91cd\\u65b0\\u5b9a\\u4e49\\u88e4\\u5b50\\u6536\\u7eb3\\u3002\",\"event_end_time\":\"\",\"event_start_time\":\"\",\"event_zt\":\"\",\"event_yjbl\":\"\",\"event_yj\":\"\",\"uptime\":\"\",\"hot\":\"\",\"hit\":\"\",\"hotcolor\":\"\",\"starttime\":null,\"endtime\":null,\"status\":\"\",\"createtime\":\"1495187364\",\"dxtime\":\"0\",\"videoid\":\"0\",\"zt\":\"0\",\"dlyj\":\"2.27\"}]}"); 
            //#endregion 
            #endregion

            SolrDLIndexInfoBLL bll = new SolrDLIndexInfoBLL();
            int offset = XYD.Common.Request.GetFormInt("limit", 1);
            int classid = XYD.Common.Request.GetFormInt("cid", 0);
            string type = XYD.Common.Request.GetFormString("type");
            int sort = XYD.Common.Request.GetFormInt("sort", 0);
            string Key = XYD.Common.Request.GetFormString("Key");
            string strwhere = "(biz30day:[0 TO *])";
            if (classid != 0)//按分类查找 
            {
                strwhere += string.Format("AND(DataTypeID:{0})", classid);
            }
            switch (type)
            {
                case "k9":
                    strwhere = strwhere + " AND(zkPrice:[0 TO 10])";
                    break;
                case "k19":
                    strwhere = strwhere + " AND(zkPrice:[10 TO 20])";
                    break;
                case "k29":
                    strwhere = strwhere + " AND(zkPrice:[20 TO 30])";
                    break;
            }


            if (!string.IsNullOrEmpty(Key))
            {
                strwhere += "AND(";
                strwhere += string.Format("(productname:{0})", Key);
                strwhere += ")";
                //搜索
            }

            string Order = "couponTotalCount desc";
            switch (sort)
            {
                case 0:
                    Order = "couponTotalCount desc";//综合
                    break;
                case 1:
                    Order = "biz30day desc";//销量
                    break;
                case 2:
                    Order = "prices asc";//价格
                    break;
                case 3:
                    Order = "couponAmount desc";//优惠率
                    break;
                default:
                    break;
            }
            //爆款热销
            if (XYD.Common.Request.GetFormString("gender") != "")
            {
                Order = "biz30day desc";//销量
            }

            // offset = offset >0 ? (offset - 1) * 20 + 1 : 0;

            offset = offset > 0 ? (offset - 1) * 20 + 20 : 0;


            PagingList<ProductDLIndex> plist = bll.GetProductList(strwhere, offset, 20, Order);
            if (plist.CurrentPageList != null && plist.CurrentPageList.Count > 0)
            {
                float rate = 100 - SysConfig.rate_all - AgencyUsers.commission;
                //rate -= AgencyUsers.commission;

                plist.CurrentPageList.ForEach(l =>
                {
                    l.tkRate =(float.Parse(l.tkRate) * (rate / 100)).ToString();
                });
            }


            if (!string.IsNullOrEmpty(Key))
            {
                SearchKeyWordDAL.InsertKeyWord(1, 1, 1, Key, "", plist.Total, "", XYD.Common.Request.GetIP(), "网站搜索");
            }
            return Json(new { State = true, Message = "查询成功", total = plist.Total, rows = plist });

        }

      
    }
}